﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Starfire3d.Interfaces;

namespace Starfire3d.Data
{
    public class DataContainer : IDataContainer
    {
        private Dictionary<string, object> dataContents = new Dictionary<string, object>();

        public DataContainer()
        {
        }

        #region Public Methods
        public void ClearData()
        {
            dataContents.Clear();
        }

        public object GetData(string key)
        {
            if (dataContents.ContainsKey(key.ToUpper()))
                return dataContents[key.ToUpper()];

            return null;
        }

        public void Add<T>(string key, T value, bool overwrite)
        {
            if (!dataContents.ContainsKey(key.ToUpper()))
                dataContents.Add(key.ToUpper(), value);
            else if (overwrite)
                dataContents[key.ToUpper()] = value;
        }

        public string GetString(string key)
        {
            if (dataContents.ContainsKey(key.ToUpper()) && dataContents[key.ToUpper()] is string)
                return (dataContents[key] as string).ToUpper();

            return string.Empty;
        }

        public int GetInt(string key)
        {
            if (dataContents.ContainsKey(key.ToUpper()) && dataContents[key.ToUpper()] is int)
                return (int)dataContents[key.ToUpper()];

            return -1;
        }

        public double GetDouble(string key)
        {
            if (dataContents.ContainsKey(key.ToUpper()) && dataContents[key.ToUpper()] is double)
                return (double)dataContents[key.ToUpper()];

            return -1.0;
        }

        public bool GetBool(string key)
        {
            if (dataContents.ContainsKey(key.ToUpper()) && dataContents[key.ToUpper()] is bool)
                return (bool)dataContents[key.ToUpper()];

            return false;
        }

        public string ParseTo()
        {
            StringBuilder sb = new StringBuilder(500);
            foreach (KeyValuePair<string, object> pair in dataContents)
            {
                sb.Append("[").Append(pair.Key).Append(":");

                if (pair.Value is string)
                    sb.Append("s");
                else if (pair.Value is int)
                    sb.Append("i");
                else if (pair.Value is double)
                    sb.Append("d");
                else if (pair.Value is bool)
                    sb.Append("b");

                sb.Append(":").Append(pair.Value.ToString()).Append("]");
            }

            return sb.ToString();
        }

        public void ParseFrom(string text)
        {
            string[] pairs = text.Split(new Char[] {']'});

            foreach (string pair in pairs)
            {
                string[] values = pair.Split(new Char[] {':'});

                if (values.Length == 3)
                {
                    if( values[1] == "s")
                        Add(values[0].Substring(1), values[2], true);
                    else if (values[1] == "i")
                    {
                        int value = int.Parse(values[2]);
                        Add(values[0].Substring(1), value, true);
                    }
                    else if (values[1] == "d")
                    {
                        double value = double.Parse(values[2]);
                        Add(values[0].Substring(1), value, true);
                    }
                    else if (values[1] == "b")
                    {
                        bool value = bool.Parse(values[2]);
                        Add(values[0].Substring(1), value, true);
                    }
                }
            }
        }
        #endregion
    }
}
